package com.example.rxpermissiondemo;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private final static int CAMERA_REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_carme)
    public void onCameraClicked() {
        // 检测是否有权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            // 没有权限，则申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_REQUEST_CODE);
        } else {
            // 如果有相机权限
            Toast.makeText(MainActivity.this, "有权限，可以拍照了！", Toast.LENGTH_LONG).show();
        }
    }

    @OnClick(R.id.btn2)
    public void onTestPermission() {
        Log.e(TAG, "onTestPermission, READ_EXTERNAL_STORAGE:" + ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE));
        Log.e(TAG, "onTestPermission, WRITE_EXTERNAL_STORAGE:" + ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE));
        new Thread(new Runnable() {
            @Override
            public void run() {
                File file1 = new File(Environment.getExternalStorageDirectory() + "/tt.txt");
                try {
                    FileWriter fileWriter = new FileWriter(file1, true);
                    fileWriter.append("haha");
                    fileWriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }).start();

    }

    /**
     * 对申请权限对回调处理
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case CAMERA_REQUEST_CODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 授予了权限
                    Log.e(TAG, "用户点击了允许，授予了权限" );
                } else  {
                    // 没有授予权限
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                        // 需要弹框解释(用户点击了拒绝，但是没有勾选"不再询问")
                        Log.e(TAG, "用户点击了拒绝，但是没有勾选不再询问" );
                    } else {
                        // 不需要弹框解释(用户点击了拒绝，但是勾选了"不再询问")
                        Log.e(TAG, "用户点击了拒绝，但是勾选了不再询问" );
                    }
                }
                break;
            default:
                break;
        }
    }
}

